package com.hdy.javachatroomdemo.advice;

import com.hdy.javachatroomdemo.Enum.ResultCodeEnum;
import com.hdy.javachatroomdemo.Model.Result;
import com.hdy.javachatroomdemo.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

@ControllerAdvice
@RestController
@Slf4j
public class ExceptionHandle {
    @ExceptionHandler(ServiceException.class)
    public Result exceptionHandle(ServiceException e) {
        log.error("发生错误:{} ",e.getMsg());
        return Result.fail(e.getMsg(),e.getCode());
    }
    @ExceptionHandler(RuntimeException.class)
    public Result exceptionHandle(RuntimeException e) {
        log.error("发生错误:{} ",e);
        return Result.fail(ResultCodeEnum.FAIL);
    }
    @ExceptionHandler(Exception.class)
    public Result exceptionHandle(Exception e) {
        log.error("发生错误:{} ",e);
        return Result.fail(ResultCodeEnum.FAIL);
    }

}
